﻿using Ljy_VoteSys.Models.Database;
using Ljy_VoteSys.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Ljy_VoteSys.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]

    public class AddTypeController : Controller
    {
        //定义数据库上下文
        private readonly Ljy_VoteSysContext _db;
        //注入jwt服务
        private readonly IJWTService _jwtService;

        public string companyUser => Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value;

        public AddTypeController(IJWTService jwtService, Ljy_VoteSysContext db)
        {
            _jwtService = jwtService;
            _db = db;
        }
        //工程类、技术类、产品类
        /// <summary>
        /// 企业用户工程类添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]

        public string test1(CompanyDetail request)
        {
            var check = _db.CompanyUsers.FirstOrDefault(x => x.CompanyUser1 == companyUser);
            if(check==null)
                return "你不是企业用户哦";
            var data = _db.CompanyDetails.FirstOrDefault(x => x.CompanyUser == companyUser && x.ClassId == 1);
            if (data != null)
                return "是 数据已经添加过 ";
            var CompanyDetail = new CompanyDetail
            {
                CompanyUser = companyUser,
                CompanyIntroduce = request.CompanyIntroduce,
                CompanyName = request.CompanyName,
                CompanyLogoUrl = request.CompanyLogoUrl,
                Level = request.Level,
                ClassId = 1,
                IsShow = false,
                Testid = request.CompanyName + "1"
            };
            _db.CompanyDetails.Add(CompanyDetail);
            var row = _db.SaveChanges();
            if (row > 0)
                return "添加成功,请等待审核";
            return "添加失败";


        }
        /// <summary>
        /// 企业用户技术类添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]

        public string test2(CompanyDetail request)
        {
            var check = _db.CompanyUsers.FirstOrDefault(x => x.CompanyUser1 == companyUser);
            if (check == null)
                return "你不是企业用户哦";
            var data = _db.CompanyDetails.FirstOrDefault(x => x.CompanyUser == companyUser && x.ClassId == 2);
            if (data != null)
                return "是 数据已经添加过 ";
            var CompanyDetail = new CompanyDetail
            {
                CompanyUser = companyUser,
                CompanyIntroduce = request.CompanyIntroduce,
                CompanyName = request.CompanyName,
                CompanyLogoUrl = request.CompanyLogoUrl,
                Level = request.Level,
                ClassId = 2,
                IsShow = false,
                Testid= request.CompanyName+"2"
            };
            _db.CompanyDetails.Add(CompanyDetail);
            var row = _db.SaveChanges();
            if (row > 0)
                return "添加成功,请等待审核";
            return "添加失败";


        }
        /// <summary>
        /// 企业用户技术类添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]

        public string test3(CompanyDetail request)
        {
            var check = _db.CompanyUsers.FirstOrDefault(x => x.CompanyUser1 == companyUser);
            if (check == null)
                return "你不是企业用户哦";
            var data = _db.CompanyDetails.FirstOrDefault(x => x.CompanyUser == companyUser && x.ClassId == 3);
            if (data != null)
                return "是 数据已经添加过 ";
            var CompanyDetail = new CompanyDetail
            {
                CompanyUser = companyUser,
                CompanyIntroduce = request.CompanyIntroduce,
                CompanyName = request.CompanyName,
                CompanyLogoUrl = request.CompanyLogoUrl,
                Level = request.Level,
                ClassId = 3,
                IsShow = false,
                Testid = request.CompanyName + "3"
            };
            _db.CompanyDetails.Add(CompanyDetail);
            var row = _db.SaveChanges();
            if (row > 0)
                return "添加成功,请等待审核";
            return "添加失败";


        }

        //审核接口
        /// <summary>
        /// 企业用户审核
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpPut]
        public string checkCompany(string CompanyName, int ClassId, bool IsShow)
        {
            var data = _db.CompanyDetails.FirstOrDefault(x => x.CompanyName == CompanyName && x.ClassId == ClassId);
            if (data == null)
            {
                return "当前不存在这个数据";
            }
            data.IsShow = IsShow;
            if (IsShow == false) return "审核失败";
            var row = _db.SaveChanges();
            if (row > 0)
                checkCompanysuccess(CompanyName, ClassId);
            return "审核通过"+ data.IsShow+data.CompanyName+data.ClassId;
            return "审核失败";

        }
        [HttpPost]
        private string checkCompanysuccess(string CompanyName, int ClassId)
        {
            var ticketobj = new Ticket
            {
                CompanyName = CompanyName,
                TotalTicket = 0,
                Classid = ClassId
            };
            _db.Tickets.Add(ticketobj);
            var row = _db.SaveChanges();
            if (row > 0)
            return "审核通过";
            return "审核失败";
        }
    

    }
    }

